package turtle.system.doug.SVMSystem;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import turtle.system.pt.Bar;
import turtle.system.pt.BarSet;
import arbor.mining.algorithm.svm.svm_predict;
import arbor.mining.algorithm.svm.svm_scale;

public class SVMWriter {
	public static final String TrainingDayEnd = "2006/01/01";

	DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
	Date trainEnd = null;
	BufferedWriter trainWriter = null, testWriter = null;

	public SVMWriter(String trainFile, String testFile) {
		try {
			trainEnd = df.parse(TrainingDayEnd);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			trainWriter = new BufferedWriter(new FileWriter(trainFile));
			testWriter = new BufferedWriter(new FileWriter(testFile));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void writerSVM(BarSet barSet, BarSet barSet2) {

		int nextIndex = 100;
		while (nextIndex < barSet.size() - 2) {
			StringBuffer buf = new StringBuffer();
			Bar yesyesBar = barSet.getBar(nextIndex - 3);
			Bar yesyesBar2 = barSet2.getBar(nextIndex - 3);

			Bar yesBar = barSet.getBar(nextIndex - 2);
			Bar yesBar2 = barSet2.getBar(nextIndex - 2);
			boolean ret = false;
			Bar bar = barSet.getBar(nextIndex - 1);
			Bar bar2 = barSet2.getBar(nextIndex - 1);

			Bar nextBar = barSet.getBar(nextIndex);
			Bar nextBar2 = barSet2.getBar(nextIndex);
			buf.append(nextBar2.open > nextBar2.close ? "1" : "-1");

			buf.append(" 1:"+ (String.format("%.5f", bar2.open)));
			buf.append(" 2:"+ (String.format("%.5f", bar2.high)));
			buf.append(" 3:"+ (String.format("%.5f", bar2.low)));
			buf.append(" 4:"+ (String.format("%.5f", bar2.close)));
			buf.append(" 5:"+ (String.format("%.5f", yesBar2.open)));
			buf.append(" 6:"+ (String.format("%.5f", yesBar2.high)));
			buf.append(" 7:"+ (String.format("%.5f", yesBar2.low)));
			buf.append(" 8:"+ (String.format("%.5f", yesBar2.close)));
			buf.append(" 9:"+ (String.format("%.5f", yesyesBar2.open)));
			buf.append(" 10:"+ (String.format("%.5f", yesyesBar2.high)));
			buf.append(" 11:"+ (String.format("%.5f", yesyesBar2.low)));
			buf.append(" 12:"+ (String.format("%.5f", yesyesBar2.close)));
			buf.append(" 13:"+ (String.format("%.5f", bar2.adx)));
			buf.append(" 14:"+ (String.format("%.5f", yesBar2.adx)));
			buf.append(" 15:"+ (String.format("%.5f", yesyesBar2.adx)));
			
			buf.append(" 21:"+ (String.format("%.5f", bar.open)));
			buf.append(" 22:"+ (String.format("%.5f", bar.high)));
			buf.append(" 23:"+ (String.format("%.5f", bar.low)));
			buf.append(" 24:"+ (String.format("%.5f", bar.close)));
			buf.append(" 25:"+ (String.format("%.5f", yesBar.open)));
			buf.append(" 26:"+ (String.format("%.5f", yesBar.high)));
			buf.append(" 27:"+ (String.format("%.5f", yesBar.low)));
			buf.append(" 28:"+ (String.format("%.5f", yesBar.close)));
			buf.append(" 29:"+ (String.format("%.5f", yesyesBar.open)));
			buf.append(" 310:"+ (String.format("%.5f", yesyesBar.high)));
			buf.append(" 311:"+ (String.format("%.5f", yesyesBar.low)));
			buf.append(" 312:"+ (String.format("%.5f", yesyesBar.close)));
			buf.append(" 313:"+ (String.format("%.5f", bar.volume)));
			buf.append(" 314:"+ (String.format("%.5f", yesBar.volume)));
			buf.append(" 315:"+ (String.format("%.5f", yesyesBar.volume)));
			buf.append(" 316:"+ (String.format("%.5f", bar.adx)));
			buf.append(" 317:"+ (String.format("%.5f", yesBar.adx)));
			buf.append(" 318:"+ (String.format("%.5f", yesyesBar.adx)));			
/*			
			buf.append(" 1:"+ (String.format("%.5f", bar2.open-bar2.close)));
			buf.append(" 2:"+ (String.format("%.5f", bar2.high-bar2.close)));	
			buf.append(" 3:"+ (String.format("%.5f", bar2.high-bar2.low)));			
			buf.append(" 4:"+ (String.format("%.5f", bar2.high-bar2.open)));
			buf.append(" 5:"+ (String.format("%.5f", bar2.close-bar2.low)));	
			buf.append(" 6:"+ (String.format("%.5f", bar2.open-bar2.low)));
			
			buf.append(" 7:"+ (String.format("%.5f", yesBar2.open-yesBar2.close)));
			buf.append(" 8:"+ (String.format("%.5f", yesBar2.high-yesBar2.close)));	
			buf.append(" 9:"+ (String.format("%.5f", yesBar2.high-yesBar2.low)));			
			buf.append(" 10:"+ (String.format("%.5f", yesBar2.high-yesBar2.open)));
			buf.append(" 11:"+ (String.format("%.5f", yesBar2.close-yesBar2.low)));	
			buf.append(" 12:"+ (String.format("%.5f", yesBar2.open-yesBar2.low)));
			
			buf.append(" 13:"+ (String.format("%.5f", bar2.open-yesBar2.close)));	
			buf.append(" 14:"+ (String.format("%.5f", bar2.high-yesBar2.high)));
			buf.append(" 15:"+ (String.format("%.5f", bar2.low-yesBar2.low)));
			buf.append(" 16:"+ (String.format("%.5f", bar2.close-yesBar2.close)));
			buf.append(" 17:"+ (String.format("%.5f", bar2.high-yesBar2.low)));
			buf.append(" 18:"+ (String.format("%.5f", bar2.low-yesBar2.high)));

			buf.append(" 19:"+ (String.format("%.5f", bar.volume-yesBar.volume)));
			buf.append(" 20:"+ (String.format("%.5f", yesBar.volume-yesyesBar.volume)));
*/
			
			try {
				if (bar2.today.getTime().before(trainEnd)) {
					trainWriter.append(buf.toString() + "\n");
				} else {
					testWriter.append(buf.toString() + "\n");
				}
				nextIndex++;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			trainWriter.close();
			testWriter.close();
		} catch (IOException e) {
			;
		}
	}
}
